Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class DLGexport
	Inherits System.Windows.Forms.Form
	
	Private sFilePath As String
	
	Dim i As Short
	Dim j As Short
	
	' Form load event ----------------------------------------------------------------------------------------------------------------------
	
	Private Sub DLGexport_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		
		initForm()
		
	End Sub
	
	' Initialize the form. Set the load the fields list, with the fields in the main form's listbox -------------------------------------
	
	Public Sub initForm()
		
		On Error Resume Next
		
		Dim aSplitDefaultExportFields As Object
		Dim sDefaultExportFieldsList As String
		
		LISTfields.Items.Clear()
		
		For i = 0 To FRMmain.LISTfields.Items.Count - 1
			
			LISTfields.Items.Add(VB6.GetItemString(FRMmain.LISTfields, i))
			
		Next 
		
		' Select the default export fields -----------------------------------------------------------------------
		
		'UPGRADE_WARNING: Couldn't resolve default property of object getPref(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		sDefaultExportFieldsList = getPref("", "defaultExportFields")
		
		'UPGRADE_WARNING: Couldn't resolve default property of object aSplitDefaultExportFields. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		aSplitDefaultExportFields = Split(sDefaultExportFieldsList, ",")
		
		For i = 0 To Me.LISTfields.Items.Count
			
			For j = 0 To UBound(aSplitDefaultExportFields)
				
				'UPGRADE_WARNING: Couldn't resolve default property of object aSplitDefaultExportFields(j). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				If aSplitDefaultExportFields(j) = VB6.GetItemString(LISTfields, i) Then
					
					LISTfields.SetSelected(i, True)
					
					Exit For
					
				End If
				
			Next 
			
		Next 
		
		TXTexportPath.Text = getDefaultExportFilePath
		
		'UPGRADE_WARNING: Couldn't resolve default property of object ProgressBar.Visible. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		ProgressBar.Visible = False
		
		CMDExport.Enabled = True
		
	End Sub
	
	Private Function export() As Boolean
		
		Dim bExported As Boolean
		
		' Validate file name and field selection, and call the export method in the export module ------------------------------------------
		
		' Check that at least one field is selected in the listbox ------------------------------------------------------------------------------
		
		If LISTfields.SelectedItems.Count < 1 Then
			
			MsgBox("Please Choose at Least One Field To Export")
			
			Exit Function
			
		End If
		
		' Check for a file name ------------------------------------------------------------------------------------------
		
		If TXTexportPath.Text = "" Then
			
			MsgBox("Please Type A Name For The File")
			TXTexportPath.Focus()
			
			Exit Function
			
		End If
		
		Dim sFieldsToExport As String
		
		If Trim(VB6.GetItemString(LISTfields, LISTfields.SelectedIndex)) = "All" Then
			
			sFieldsToExport = "!All!"
			
		Else
			
			For i = 0 To LISTfields.Items.Count - 1
				
				If LISTfields.GetSelected(i) = True Then
					
					sFieldsToExport = sFieldsToExport & "[" & VB6.GetItemString(LISTfields, i) & "],"
					
				End If
				
			Next 
			
			' Remove the last 'comma' --------------------------------------------------------------------------------------
			
			sFieldsToExport = VB.Left(sFieldsToExport, Len(sFieldsToExport) - 1)
			
		End If
		
		If RADIOexportToExcel.Checked = True Then
			
			' Export to Excel file ----------------------------------------------------------------------------------------------------
			
            'bExported = MODexport.exportToExcel(VB6.GetItemString(FRMmain.LISTtables, FRMmain.LISTtables.SelectedIndex), sFieldsToExport, (TXTexportPath.Text))
			
		Else
			
			' Export to text file -------------------------------------------------------------------------------------------------------
			
			'UPGRADE_WARNING: Couldn't resolve default property of object MODexport.exportToText(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            'bExported = MODexport.exportToText(VB6.GetItemString(FRMmain.LISTtables, FRMmain.LISTtables.SelectedIndex), sFieldsToExport, (TXTexportPath.Text))
			
		End If
		
		export = bExported
		
	End Function
	
	
	' Build a path for the file to be exported -------------------------------------------------------------------------------------------------------
	
	Private Function getDefaultExportFilePath() As Object
		
		On Error Resume Next
		
		Dim sFileName As String
		
		sFileName = VB6.GetItemString(FRMmain.LISTtables, FRMmain.LISTtables.SelectedIndex)
		
		sFilePath = My.Application.Info.DirectoryPath & "\" & sFileName & "_" & VB.Day(Now) & "_" & Month(Now) & ".txt"
		
		'UPGRADE_WARNING: Couldn't resolve default property of object getDefaultExportFilePath. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		getDefaultExportFilePath = sFilePath
		
	End Function
	
	' When user clicks browse to choose a path for exporting -----------------------------------------------------------------------------------
	
	Private Sub CMDbrowse_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CMDbrowse.Click
		
		On Error GoTo errorHandler
		
        With FRMmain.CommonDialogOpen

            .FileName = getDefaultExportFilePath()

            'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
            .Filter = "Text Documents (*.txt)|*.txt|Excel (*.xls)|*.xls|All Files|*.*"

            .ShowDialog()

            TXTexportPath.Text = .FileName

        End With
		
		Exit Sub
		
errorHandler: 
		
		Select Case Err.Number
			
			Case 32755 ' Cancel was selected
				
				Exit Sub
				
			Case 20477 ' Invalid file name
				
				MsgBox("The path """ & sFilePath & """ is invalid")
				
				Exit Sub
				
		End Select
		
	End Sub
	
	' Select all items when user clicks 'All' -------------------------------------------------------------------------------------------------
	
	'UPGRADE_WARNING: Event LISTfields.SelectedIndexChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub LISTfields_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles LISTfields.SelectedIndexChanged
		
		On Error Resume Next
		
		If LISTfields.GetSelected(0) = True Then
			
			For i = 1 To LISTfields.Items.Count - 1
				
				LISTfields.SetSelected(i, True)
				
			Next 
			
		End If
		
	End Sub
	
	' Export the database ---------------------------------------------------------------------------------------------------
	
	Private Sub CMDExport_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CMDExport.Click
		
		On Error Resume Next
		
		If export Then Me.Hide()
		
	End Sub
	
	'UPGRADE_WARNING: Event RADIOexportToText.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub RADIOexportToText_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles RADIOexportToText.CheckedChanged
		If eventSender.Checked Then
			
			On Error Resume Next
			
			If LCase(VB.Right(TXTexportPath.Text, 4)) = ".xls" Then
				
				TXTexportPath.Text = VB.Left(TXTexportPath.Text, Len(TXTexportPath.Text) - 4) & ".txt"
				
			Else
				
				TXTexportPath.Text = TXTexportPath.Text & ".txt"
				
			End If
			
		End If
	End Sub
	'UPGRADE_WARNING: Event RADIOexportToExcel.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub RADIOexportToExcel_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles RADIOexportToExcel.CheckedChanged
		If eventSender.Checked Then
			
			On Error Resume Next
			
			If LCase(VB.Right(TXTexportPath.Text, 4)) = ".txt" Then
				
				TXTexportPath.Text = VB.Left(TXTexportPath.Text, Len(TXTexportPath.Text) - 4) & ".xls"
				
			Else
				
				TXTexportPath.Text = TXTexportPath.Text & ".xls"
				
			End If
			
		End If
	End Sub
	
	Private Sub CMDcancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CMDcancel.Click
		
		Me.Close()
		
	End Sub
End Class